<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%project}}".
 *
 * @property int $id 序号
 * @property string $name 项目名称
 * @property int $origin_type 项目来源：1-合同签订 2-委托书 3-中标通知书 4-口头订单
 * @property int $branch_id 承建分公司
 * @property int $leading_user_id 生产负责人
 * @property int $plan_started_at 项目生产开始时间
 * @property int $plan_completed_at 项目生产计划完成时间
 * @property int $progress_status 0-进行中 1-已完成
 * @property string $content 项目内容
 * @property int $input_user_id 录入员
 * @property int $created_at 记录创建时间
 * @property int $updated_at 记录修改时间
 * @property int $valid 1-有效 0-无效
 * @property string $money_total 合同金额
 * @property string $money_received 已收款金额
 * @property string $money_pay_way 分几期如何付
 * @property int $customer_id 甲方客户id
 * @property int $fee_percentage 百分之几的管理费
 * @property int $contract_type 合同类型1-自主2-挂靠3-分包
 */
class Project extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['origin_type', 'branch_id', 'leading_user_id', 'plan_started_at', 'plan_completed_at', 'progress_status', 'input_user_id', 'created_at', 'updated_at', 'valid', 'customer_id', 'fee_percentage',
                'contract_type','sign_status','sign_at','complete_at','flow_status', 'design_stage', 'figure', 'project_type_id', 'department_id','receive_status'], 'integer'],
            [['content', 'checker', 'proofreader', 'planner', 'project_no', 'budgeter', 'team_worker', 'pay_way'], 'string'],
            [['money_total', 'money_received', 'research_cost', 'early_cost', 'design_cost', 'make_cost', 'project_total','project_cost', 'project_complete', 'prospect_cost'], 'number'],
            [['name'], 'string', 'max' => 255],
            [['name'], 'unique'],
        ];
    }

    public function extraFields()
    {
        $fields['leading_user_name'] = function ($model){
            return $model->leadingUser->username;
        };
        $fields['customer_name'] = function ($model){
            return $model->customer->name;
        };

        $fields['flowType'] = function ($model){
            return $model->flowType;
        };

        $fields['department'] = function ($model){
            return $model->department;
        };
        $fields['report'] = function ($model){
            return $model->report;
        };

        $fields['projectFh'] = function ($model){
            return $model->projectFh;
        };
        $fields['projectYt'] = function ($model){
            return $model->projectYt;
        };

        $fields['flowHistory'] = function ($model){
            return $model->flowHistory;
        };
        return $fields;
    }

    public function getLeadingUser(){
        return $this->hasOne(User::class,['id'=>'leading_user_id']);
    }

    public function getCustomer(){
        return $this->hasOne(Customer::class,['id'=>'customer_id']);
    }

    public function getFlowType(){
        return $this->hasOne(FlowType::class,['project_id'=>'id']);
    }

    public function getDepartment(){
        return $this->hasOne(Department::class,['id'=>'department_id']);
    }

    public function getProjectFh(){
        return $this->hasOne(ProjectFh::class,['project_id'=>'id']);
    }

    public function getProjectYt(){
        return $this->hasOne(ProjectYt::class,['project_id'=>'id']);
    }

    public function getFlowHistory(){
        return $this->hasMany(FlowHistory::class,['project_id'=>'id']);
    }

    public function getReport(){
        return $this->hasMany(Report::class,['department_id'=>'department_id']);
    }

    // 统计各种条件下项目数量
    public static function projectSum($params) {
        $projectSum = Project::find()->where(['contract_type' => $params['contract_type']]);
        if (isset($params['progress_status'])) {
            $projectSum->andWhere(['progress_status' => $params['progress_status']]);
        }

        if (isset($params['valid'])) {
            $projectSum->andWhere(['valid' => $params['valid']]);
        }

        if (isset($params['branch_id'])) {
            $projectSum->andWhere(['branch_id' => $params['branch_id']]);
        }
        $projectSum->andWhere(['>=', 'created_at', $params['start']]);
        $projectSum->andWhere(['<=', 'created_at', $params['end']]);
        $result = $projectSum->count();
        return $result;
    }

    // 统计合同金额,
    // 统计合同金额,根据条件查询合同金额
    public static function contractSum($params, $fields) {
        $projectSum = Project::find();
        if (isset($params['contract_type'])) {
            $projectSum->andWhere(['contract_type' => $params['contract_type']]);
        }
        if (isset($params['department_id'])) {
            $projectSum->andWhere(['department_id' => $params['department_id']]);
        }

        if (isset($params['branch_id'])) {
            $projectSum->andWhere(['branch_id' => $params['branch_id']]);
        }
        if (isset($params['valid'])) {
            $projectSum->andWhere(['valid' => $params['valid']]);
        }
        $projectSum->andWhere(['>=', 'created_at', $params['start']]);
        $projectSum->andWhere(['<=', 'created_at', $params['end']]);
        $sum = $projectSum->sum($fields);
        return $sum;
    }

    // 统计可收款
    public static function costHistory($params) {
        $projectSum = Project::find()->where(['valid' => 1]);
        $projectSum->andWhere(['>=', 'created_at', $params['start']]);
        $projectSum->andWhere(['<=', 'created_at', $params['end']]);
        $projectSum = $projectSum->all();
        $hisOne = 0;
        $hisTwo = 0;
        $hisThree = 0;
        foreach ($projectSum as $k => $v) {

            if ($v['contract_type'] == 1) {
                $hisOne +=$v['money_total'] + $v['money_total'] * ($v['fee_percentage']/100) - $v['money_received'];
            }

            if ($v['contract_type'] == 2) {
                $hisTwo +=$v['money_total'] + $v['money_total'] * ($v['fee_percentage']/100)- $v['money_received'];
            }

            if ($v['contract_type'] == 2) {
                $hisThree +=$v['money_total'] + $v['money_total'] * ($v['fee_percentage']/100) - $v['money_received'];
            }
        }
        return [
            'hisOne' => $hisOne,
            'hisTwo' => $hisTwo,
            'hisThree' => $hisThree
        ];
    }

    // 获取当前时间的4个季度
    public static function Quarter() {
        $season = ceil(date('n')/3); // date('n')获取当前月份

        if ($season == 1) {
            $starOne =  date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
            $endOne =date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));

            $starTwo =  date('Y-m-01',mktime(0,0,0,$season *3 +1,1,date('Y')));
            $endTwo =date('Y-m-t',mktime(0,0,0,($season + 1) * 3,1,date('Y')));

            $starThree =  date('Y-m-01',mktime(0,0,0,($season + 1) *3 +1,1,date('Y')));
            $endThree = date('Y-m-t',mktime(0,0,0,($season + 2) * 3,1,date('Y')));

            $starFour =  date('Y-m-01',mktime(0,0,0,($season + 2) +1,1,date('Y')));
            $endFour = date('Y-m-t',mktime(0,0,0,($season + 3) * 3,1,date('Y')));
        }

        if ($season == 2) {
            $starOne =  date('Y-m-01',mktime(0,0,0,($season - 2) *3 +1,1,date('Y')));
            $endOne =date('Y-m-t',mktime(0,0,0,($season - 1) * 3,1,date('Y')));

            $starTwo =  date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
            $endTwo =date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));

            $starThree =  date('Y-m-01',mktime(0,0,0,$season *3 +1,1,date('Y')));
            $endThree =date('Y-m-t',mktime(0,0,0,($season + 1) * 3,1,date('Y')));

            $starFour =  date('Y-m-01',mktime(0,0,0,($season + 1) *3 +1,1,date('Y')));
            $endFour = date('Y-m-t',mktime(0,0,0,($season + 2) * 3,1,date('Y')));
        }

        if ($season == 3) {
            $starOne =  date('Y-m-01',mktime(0,0,0,($season - 3) *3 +1,1,date('Y')));
            $endOne =date('Y-m-t',mktime(0,0,0,($season - 2) * 3,1,date('Y')));

            $starTwo =  date('Y-m-01',mktime(0,0,0,($season - 2) *3 +1,1,date('Y')));
            $endTwo =date('Y-m-t',mktime(0,0,0,($season - 1) * 3,1,date('Y')));

            $starThree =  date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
            $endThree =date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));

            $starFour=  date('Y-m-01',mktime(0,0,0,$season *3 +1,1,date('Y')));
            $endFour =date('Y-m-t',mktime(0,0,0,($season + 1) * 3,1,date('Y')));
        }

        if ($season == 4) {
            $starOne =  date('Y-m-01',mktime(0,0,0,($season - 4) *3 +1,1,date('Y')));
            $endOne =date('Y-m-t',mktime(0,0,0,($season - 3) * 3,1,date('Y')));

            $starTwo =  date('Y-m-01',mktime(0,0,0,($season - 3) *3 +1,1,date('Y')));
            $endTwo =date('Y-m-t',mktime(0,0,0,($season - 2) * 3,1,date('Y')));

            $starThree =  date('Y-m-01',mktime(0,0,0,($season - 2) *3 +1,1,date('Y')));
            $endThree =date('Y-m-t',mktime(0,0,0,($season - 1) * 3,1,date('Y')));

            $starFour =  date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
            $endFour =date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));
        }

        return [
            '1' => [
                'starOne' => $starOne,
                'endOne' => $endOne,
            ] ,
            '2' => [
                'starTwo' => $starTwo,
                'endTwo' => $endTwo,
            ],
            '3' => [
                'starThree' => $starThree,
                'endThree' => $endThree,
            ],
            '4' => [
                'starFour' => $starFour,
                'endFour' => $endFour,
            ]

        ];
    }

}